﻿using Admin.CommonLib.Models.Role;
using FluentValidation;

namespace Admin.CommonLib.Validators
{
    public class RoleEditValidator : AbstractValidator<RoleEditRequestDto>
    {
        public RoleEditValidator()
        {
            RuleFor(p => p.RoleCode)
                .NotEmpty()
                .WithMessage("角色编码为必填项");

            RuleFor(p => p.RoleName)
                .NotEmpty()
                .WithMessage("角色名称为必填项");
        }

        public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
        {
            var result = await ValidateAsync(ValidationContext<RoleEditRequestDto>.CreateWithOptions((RoleEditRequestDto)model, x => x.IncludeProperties(propertyName)));
            if (result.IsValid)
                return Array.Empty<string>();
            return result.Errors.Select(e => e.ErrorMessage);
        };
    }
}